/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package control;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.Map;
import java.util.TreeSet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import modele.*;

/**
 *
 * @author ltata
 */
public class Control extends HttpServlet {

    private Pays p;


    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        p = new Pays();
        

    }

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String page;
        String action = request.getParameter("action");
        if (action == null) {
            Collection<Deps> cold = p.getLesDeps();
            request.setAttribute("lesDeps", cold);
            Collection<Specialite> cspe = p.getLesSpes();
            request.setAttribute("lesSpes", cspe);
            page = "listeDeps.jsp";
        } else if (action.equals("pardep")) {
            String choix = request.getParameter("choix");
            Collection<Med> colm = p.getLeDep(choix).getLesMeds();
            request.setAttribute("lesMeds", colm);
            page = "listeMed.jsp";
        
        
        }else if (action.equals("parspe")) {
            String choix = request.getParameter("choix");
            Collection<Med> colm = p.getLaSpe(choix).getLesMeds();
            request.setAttribute("lesMeds", colm);
            page = "parSpe.jsp";
        
        
        } else if (action.equals("parnom")){
            String nom = request.getParameter("nom");
            Collection<Med> lesMeds = new TreeSet<Med>();
            for (Deps unDep : p.getLesDeps()) {
                for (Med unMed : unDep.getLesMeds()) {
                    if(unMed.getNom().startsWith(nom)) {
                        lesMeds.add(unMed);
                    }
                }
            }
            request.setAttribute("lesMeds", lesMeds);
           page = "parNom.jsp";
        } else {
            //erreur, page1
            Collection<Deps> cold = p.getLesDeps();
            request.setAttribute("lesDeps", cold);
            page = "listeDeps.jsp";
        }
        request.getRequestDispatcher(page).forward(request, response);
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *   /
     *
     **
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
